package com.rzjm.api.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.rzjm.api.constant.ResponseCode;
import com.rzjm.api.entity.*;
import com.rzjm.api.service.*;
import com.rzjm.api.util.GsonResultBean;
import com.rzjm.api.util.Tools;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 *@Author: ZhouWei
 *@Description:  调查问卷相关接口
 *@Date 2018-05-10
 */
@Component
@RestController
@RequestMapping("/fire/work/record")//url:模块/资源/{}/细分
public class FireWorkRecordController extends BaseController {

    @Resource
    private UserService userService;
    @Resource
    private ShopService shopService;
    @Resource
    private FireWorkRecordService fireWorkRecordService;

    /**
     * 上报记录
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/add",method = {RequestMethod.POST},produces="application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject addRecord(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        Map<String,Object> map = new HashMap<>();
        try{
            logBefore(logger, "fire/work/record/add接收的参数： " + requestParam);
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            int user_id = jsonRequest.containsKey("user_id") ? jsonRequest.getInt("user_id") : 0;

            int shop_id = jsonRequest.containsKey("shop_id") ? jsonRequest.getInt("shop_id") : 0;
            String real_name = jsonRequest.containsKey("real_name") ? jsonRequest.getStr("real_name") : null;
            String phone_num = jsonRequest.containsKey("phone_num") ? jsonRequest.getStr("phone_num") : null;
            String address = jsonRequest.containsKey("address") ? jsonRequest.getStr("address") : null;
            String id_no = jsonRequest.containsKey("id_no") ? jsonRequest.getStr("id_no") : null;
            String remark = jsonRequest.containsKey("remark") ? jsonRequest.getStr("remark") : null;


            User user = userService.findById(user_id);
            if(user == null){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"该用户不存在"),true,true);
            }


            Shop shop = shopService.findById(shop_id);
            if(shop == null){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"该商家不存在"),true,true);
            }


            if(shop.getState() == 0){
                System.out.println(shop.getShopName() + " 企业已被禁用");
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"企业已被禁用"),true,true);
            }

            FireWorkRecord record = new FireWorkRecord();
            record.setUserId(user_id);
            record.setCreateTime(new Date());
            record.setShopId(shop_id);
            record.setShopName(shop.getShopName());
            record.setIdNo(id_no);
            record.setAddress(address);
            record.setPhoneNum(phone_num);
            record.setRealName(real_name);
            record.setRemark(remark);
            record.setState(1);
            boolean bl = fireWorkRecordService.save(record);
            if(bl){
                result = Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"提交成功"),true,true);
            }else{
                result = Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"提交失败"),true,true);
            }

        }catch (Exception e){
            result = Tools.getJson(new GsonResultBean<>(-1,"系统异常",e.getMessage()),true,true);
            System.out.println(e.getMessage());
            logger.error(e.getMessage(), e);
        }
        return result;
    }


}
